﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RarExtractor.Properties;

namespace RarExtractor
{
    public partial class ExtendedListBox : UserControl
    {
        [Localizable( true )]
        [MergableProperty( false )]
        [Editor(
            "System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            , typeof( UITypeEditor ) )]
        [DesignerSerializationVisibility( DesignerSerializationVisibility.Content )]
        public List<string> Items { get; set; }

        public ExtendedListBox ()
        {
            InitializeComponent();
            Items = new List<string>();
        }


        public void Add ( string item )
        {
            if (!Items.Contains( _textBoxNewItem.Text ))
            {
                _listBoxItems.Items.Add( _textBoxNewItem.Text );
                Items.Add( _textBoxNewItem.Text );
            }
        }

        private void ListBoxItemsSelectedIndexChanged ( object sender, EventArgs e )
        {
            _buttonRemove.Enabled = _listBoxItems.SelectedIndex > -1;
        }

        private void TextBoxNewItemTextChanged ( object sender, EventArgs e )
        {
            _buttonAdd.Enabled = !string.IsNullOrEmpty( _textBoxNewItem.Text );
        }

        private void ButtonAddClick ( object sender, EventArgs e )
        {
            if (!Items.Contains( _textBoxNewItem.Text ))
            {
                _listBoxItems.Items.Add( _textBoxNewItem.Text );
                Items.Add( _textBoxNewItem.Text );
            }
            else
            {
                MessageBox.Show( Resources.List_already_contains_item );
            }
        }
    }
}
